package baiqi.sishen.community.service;

import baiqi.sishen.community.enums.NotificationStatusEnum;
import baiqi.sishen.community.enums.NotificationTypeEnum;
import baiqi.sishen.community.enums.ThumbTypeEnum;
import baiqi.sishen.community.execption.CustomizeErrorCode;
import baiqi.sishen.community.execption.CustomizeException;
import baiqi.sishen.community.mapper.*;
import baiqi.sishen.community.model.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 兕神
 * @version 1.0
 * DateTime: 2023/5/7
 */
@Service
public class ThumbService {

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private CommentExtMapper commentExtMapper;

    @Resource
    private QuestionMapper questionMapper;

    @Resource
    private QuestionExtMapper questionExtMapper;

    @Resource
    private ThumbMapper thumbMapper;

    @Resource
    private NotificationMapper notificationMapper;

    /**
     * 点赞
     *
     * @param thumb thumb
     * @param liker 点赞者
     * @return int
     */
    public int insertOrUpdate(Thumb thumb, User liker) {

        //判断用户是否选中问题或评论
        if (thumb.getTargetId() == 0 || thumb.getTargetId() == null) {
            throw new CustomizeException(CustomizeErrorCode.TARGET_PARAM_NOT_FOUND);
        }
        //判断该点赞的类型是否合理
        if (thumb.getType() == null || !ThumbTypeEnum.isExist(thumb.getType())) {
            throw new CustomizeException(CustomizeErrorCode.TYPE_PARAM_WRONG);
        }

        //点赞评论
        if (thumb.getType().equals(ThumbTypeEnum.COMMENT.getType())) {
            Comment comment = commentMapper.selectByPrimaryKey(thumb.getTargetId());
            if (comment == null) {
                throw new CustomizeException(CustomizeErrorCode.COMMENT_NOT_FOUND);
            }
            //获取评论的问题
            Question question = questionMapper.selectByPrimaryKey(comment.getParentId());
            if (question == null){
                throw new CustomizeException(CustomizeErrorCode.QUESTION_NOT_FOUND);
            }

            //插入或更新点赞
            int code = insertOrUpdateByThumb(thumb);
            if (code == 2011) {
                return code;
            }
            //评论点赞数加一
            comment.setLikeCount(1L);
            commentExtMapper.incLikeCount(comment);

            //创建点赞通知
            createNotify(liker.getName(),NotificationTypeEnum.THUMB_COMMENT,
                    question.getTitle(),question.getId(),comment.getCommentator(),liker.getId(),comment.getContent());
            return code;
        } else {
            //收藏问题
            QuestionExample example = new QuestionExample();
            example.createCriteria()
                    .andIdEqualTo(thumb.getTargetId());
            List<Question> questions = questionMapper.selectByExample(example);
            if (questions.size() == 0) {
                throw new CustomizeException(CustomizeErrorCode.QUESTION_NOT_FOUND);
            }

            //插入或更新点赞
            int code = insertOrUpdateByThumb(thumb);
            if (code == 2011) {
                return code;
            }
            //问题点赞数加一
            Question question = questions.get(0);
            question.setLikeCount(1);
            questionExtMapper.incLikeCount(question);

            //创建收藏通知
            createNotify(liker.getName(),NotificationTypeEnum.COLLECT_QUESTION,
                    question.getTitle(),question.getId(),question.getCreator(),liker.getId(),null);
            return code;
        }
    }

    /**
     * 创建通知
     *
     * @param commentatorName      发通知者名字
     * @param notificationTypeEnum 通知类型是问题还是评论
     * @param questionTitle        问题题目
     * @param questionId           问题或评论的id
     * @param receiver             接受通知者id
     * @param notifier             发通知者id
     * @param commentTitle         问题的评论
     */
    private void createNotify(String commentatorName, NotificationTypeEnum notificationTypeEnum,
                              String questionTitle, Long questionId, Long receiver, Long notifier, String commentTitle) {
        Notification notification = new Notification();
        notification.setType(notificationTypeEnum.getType());
        notification.setGmtCreate(System.currentTimeMillis());

        notification.setNotifierName(commentatorName);
        notification.setQuestionTitle(questionTitle);
        notification.setQuestionId(questionId);
        notification.setCommentTitle(commentTitle);
        //设置未读
        notification.setStatus(NotificationStatusEnum.UNREAD.getStatus());
        notification.setReceiver(receiver);
        notification.setNotifier(notifier);
        //创建通知
        notificationMapper.insert(notification);
    }

    private int insertOrUpdateByThumb(Thumb thumb) {
        ThumbExample thumbExample = new ThumbExample();
        thumbExample.createCriteria()
                .andTargetIdEqualTo(thumb.getTargetId())
                .andTypeEqualTo(thumb.getType())
                .andLikerEqualTo(thumb.getLiker());
        List<Thumb> thumbs = thumbMapper.selectByExample(thumbExample);
        //无该点赞
        if (thumbs.size() == 0) {
            //插入数据
            //设定时间
            thumb.setGmtCreate(System.currentTimeMillis());
            thumb.setGmtModified(thumb.getGmtCreate());
            thumbMapper.insert(thumb);
            return 200;
        } else {
            //更新数据
            thumb = thumbs.get(0);
            thumb.setGmtModified(System.currentTimeMillis());
            thumbMapper.updateByPrimaryKey(thumb);
            return 2012;
        }
    }
}
